﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Game.model;
using Game.utils;
using ikvm.extensions;
using java.io;
using java.util;
using lineage2.commons.data.xml;

namespace Game.data
{
    public class StringHolder:AbstractHolder
{
	/**
	 * Field _instance.
	 */
	private static StringHolder _instance = new StringHolder();
	/**
	 * Field _strings.
	 */
	private Map _strings = new HashMap();
	
	/**
	 * Method getInstance.
	 * @return StringHolder
	 */
	public static StringHolder getInstance()
	{
		return _instance;
	}
	
	/**
	 * Constructor for StringHolder.
	 */
	private StringHolder()
	{
	}
	
	/**
	 * Method getNullable.
	 * @param player Player
	 * @param name String
	 * @return String
	 */
	public String getNullable(Player player, String name)
	{
		Language lang = player == null ? Language.ENGLISH : player.getLanguage();
		return get(lang, name);
	}
	
	/**
	 * Method getNotNull.
	 * @param player Player
	 * @param name String
	 * @return String
	 */
	public String getNotNull(Player player, String name)
	{
		Language lang = player == null ? Language.ENGLISH : player.getLanguage();
		String text = get(lang, name);
		if ((text == null) && (player != null))
		{
			text = "Not find string: " + name + "; for lang: " + lang;
			_strings.get(lang).put(name, text);
		}
		return text;
	}
	
	/**
	 * Method get.
	 * @param lang Language
	 * @param address String
	 * @return String
	 */
	private String get(Language lang, String address)
	{
		Map strings = _strings.get(lang) as Map;
		return strings.get(address) as string;
	}
	
	/**
	 * Method load.
	 */
	public void load()
	{
		foreach (Language lang in Language.VALUES)
		{
			_strings.put(lang, new HashMap());
			File f = new File(Config.DATAPACK_ROOT, "data/string/strings_" + lang.getShortName() + ".properties");
			if (!f.exists())
			{
				warn("Not find file: " + f.getAbsolutePath());
				continue;
			}
			LineNumberReader reader = null;
			try
			{
				reader = new LineNumberReader(new FileReader(f));
				String line = null;
				while ((line = reader.readLine()) != null)
				{
					if ((line.length() > 0) && (line.charAt(0) == '#'))
					{
						continue;
					}
					StringTokenizer token = new StringTokenizer(line, "=");
					if (token.countTokens() < 2)
					{
						error("Error on line: " + line + "; file: " + f.getName());
						continue;
					}
					String name = token.nextToken();
					String value = token.nextToken();
					while (token.hasMoreTokens())
					{
						value += "=" + token.nextToken();
					}
					Map strings = _strings.get(lang);
					strings.put(name, value);
				}
			}
			catch (Exception e)
			{
				error("Exception: " + e, e);
			}
			finally
			{
				try
				{
					reader.close();
				}
				catch (Exception e)
				{
				}
			}
		}
		log();
	}
	
	/**
	 * Method reload.
	 */
	public void reload()
	{
		clear();
		load();
	}
	
	/**
	 * Method log.
	 */
	
	public override void log()
	{
		foreach (Map.Entry entry in (IEnumerable)_strings.entrySet())
		{
			info("load strings: " + entry.getValue().size() + " for lang: " + entry.getKey());
		}
	}
	
	/**
	 * Method size.
	 * @return int
	 */
	
	public override int size()
	{
		return 0;
	}
	
	/**
	 * Method clear.
	 */
	
	public override void clear()
	{
		_strings.clear();
	}
}

}
